﻿using System;
using System.Linq;
using System.Windows.Media;
using Tanis.Controls;
using Tanis.RouteService;
using System.Collections.Generic;

namespace Tanis.DataModels
{
    public class RouteSummary
    {
        public Route Route { get; set; }

        public RouteSummary(Route route)
        {
            if (route == null) throw new ArgumentNullException("route");

            Route = route;

            if (Route.Segments.Count == 0)
                return;

            SetTimeSummary();
            SetRouteDescriptionComposite();
            SetRouteColor();
            SetRouteIcon();
        }

        private void SetRouteIcon()
        {
            RouteIcon = DesignHelper.GetIcon(Route.Segments.OfType<BusRideSegment>().FirstOrDefault());
        }

        private void SetRouteColor()
        {
            //todo: to be changed
            RouteColor = DesignHelper.GetColor(Route.Segments.OfType<BusRideSegment>().FirstOrDefault());
        }

        private void SetRouteDescriptionComposite()
        {
            var busesOnTheRouteTmp = Route.Segments.OfType<BusRideSegment>().ToList();

            List<BusRideSegment> busesOnTheRouteSegments = new List<BusRideSegment>();
            BusRideSegment prevItem = null;
            foreach (BusRideSegment item in busesOnTheRouteTmp)
            {
                if (prevItem == null)
                {
                    busesOnTheRouteSegments.Add(item);
                }
                else if (item.ScheduleName != prevItem.ScheduleName)
                {
                    busesOnTheRouteSegments.Add(item);
                }
                prevItem = item;
            }

            RouteDescriptionComposite = new List<RouteDescriptionPart>();

            var busesOnTheRoute = busesOnTheRouteSegments.Select(seg => seg.ScheduleName).ToList();
            for (int i = 0; i < busesOnTheRoute.Count; i++)
            {
                RouteDescriptionComposite.Add(new RouteDescriptionPart()
                {
                    ImagePath = DesignHelper.GetIcon(busesOnTheRouteSegments.ElementAt(i)),
                    Color = DesignHelper.GetColor(busesOnTheRouteSegments.ElementAt(i)),
                    Text = busesOnTheRoute[i] + " "
                });

                if (i < busesOnTheRoute.Count - 1)
                {
                    RouteDescriptionComposite.Add(new RouteDescriptionPart()
                    {
                        ImagePath = null,
                        Color = ThemeHelper.ThemeDependantColorInverted.ToString(),
                        Text = " > "
                    });
                }
            }
        }

        private void SetTimeSummary()
        {
            DateTime firstTime = Route.Segments.First().StartTime;
            DateTime lastTime = Route.Segments.Last().EndTime;

            var minutes = (lastTime - firstTime).Minutes;
            var hours = (lastTime - firstTime).Hours;
            string timeSummary = string.Empty;
            if (hours > 0 && minutes > 0)
            {
                timeSummary = string.Format("{0}{1} {2}{3}", hours, Localization.LocalizedStringsRes.Hours, minutes, Localization.LocalizedStringsRes.Minutes);
            }
            else if (hours > 0 && minutes == 0)
            {
                timeSummary = string.Format("{0}{1}", hours, Localization.LocalizedStringsRes.Hours);
            }
            else if (hours == 0)
            {
                timeSummary = string.Format("{0} {1}", minutes, Localization.LocalizedStringsRes.Minutes);
            }
            TimeSummary = string.Format("{0} - {1} ({2})", DesignHelper.FormatTime(firstTime), DesignHelper.FormatTime(lastTime), timeSummary);
        }

        public string TimeSummary { get; set; }
        public string RouteDescription { get; set; }
        public string RouteColor { get; set; }
        public string Transport
        {
            get { return DesignHelper.GetTransportDescription(Route.Segments.OfType<BusRideSegment>().FirstOrDefault()); }
        }
        public string RouteIcon { get; set; }

        public List<RouteDescriptionPart> RouteDescriptionComposite { get; set; }
    }

    public class RouteDescriptionPart
    {
        public string ImagePath { get; set; }
        public string Text { get; set; }
        public string Color { get; set; }
    }
}
